import fetch from "isomorphic-fetch"

function downloadSync(filePath, fileName) {
	fetch(filePath, {
		method: 'GET',
		credentials: 'include',
	}).then((response) => {
		response.blob().then(blob => {
			const reader = new FileReader();
			reader.readAsDataURL(blob);
			reader.onload = e => {
				let a = document.createElement("a");
				a.style.display = "none";
				a.download = fileName;
				a.href = e.target.result;
				document.body.appendChild(a);
				a.click();
				document.body.removeChild(a);
			};
		})
	}).catch((error) => {
		console.log(error)
	})
}


function download(filePath) {
	let ele = document.createElement("a")
	ele.setAttribute("href", filePath)
	ele.setAttribute("download", "")
	ele.click()
}

export default download
export {downloadSync}